package top.xiaoshuaila.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xiaoshuaila.pojo.*;
import top.xiaoshuaila.service.IMenuRoleService;
import top.xiaoshuaila.service.IMenuService;
import top.xiaoshuaila.service.IPositionService;
import top.xiaoshuaila.service.IRoleService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "角色管理")
@RequestMapping("/system/basic/permiss")
public class PermissionController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IMenuRoleService menuRoleService;
    @ApiOperation("获取所有角色")
    @GetMapping("/")
    private List<Role> getAllRoles(){
        return roleService.list();
    }

    @ApiOperation("添加角色")
    @PostMapping("/")
    private RespBean addRoles(@RequestBody Role role){
        if(!role.getName().startsWith("ROLE_"))
            role.setName("ROLE_"+role.getName());
        if(roleService.save(role))
            return RespBean.success("添加成功!");
        return RespBean.error("添加失败!");
    }



    @ApiOperation("删除角色")
    @DeleteMapping("/role/{rid}")
    private RespBean deleteRoles(@PathVariable Integer rid){
        if(roleService.removeById(rid))
            return RespBean.success("删除成功!");
        return RespBean.error("删除失败!");
    }

    @ApiOperation("查询所有菜单")
    @GetMapping("/menus")
    public List<Menu>getAllMenus(){
        return menuService.getAllMenus();
    }
    @ApiOperation("根据角色id查询菜单id")
    @GetMapping("/mid/{rid}")
    public List<Integer>getMidByRid(@PathVariable Integer rid){
        QueryWrapper<MenuRole>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rid",rid);
        List<Integer>list=new ArrayList<>();
        List<MenuRole> list1 = menuRoleService.list(queryWrapper);
        for(int i=0;i<list1.size();i++)
            list.add(list1.get(i).getMid());
        return list;
        //return  menuRoleService.list(queryWrapper).stream().map
        // (MenuRole::getMid).collect(Collectors.toList());
    }
    @ApiOperation("更新角色菜单")
    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid,Integer[]mids){
        return menuRoleService.updateMenuRole(rid,mids);

    }
}
